﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.project.dao.impl;

import java.util.*;

import com.hh.bss.base.*;
import com.hh.bss.common.ConstantDic;
import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.project.dao.*;
import com.hh.bss.project.entity.*;
import com.hh.bss.project.service.*;
import com.hh.bss.project.vo.query.*;
import com.hh.bss.util.*;





import static com.hh.bss.util.ObjectUtils.*;

import org.springframework.stereotype.Repository;

@Repository
public class AccountArDaoImpl extends BaseHibernateDao<AccountAr,java.lang.Integer>  implements AccountArDao<AccountAr,java.lang.Integer>{

	public Class getEntityClass() {
		return AccountAr.class;
	}
	public Page findArPage(AccountArQuery query){
		//人选姓名，职位，客户职位联系人，取款日期，项目负责人，信息录入人，手机，电邮，客户公司
		StringBuffer sql = new StringBuffer();		
		sql.append(" select * from ( ");		
		sql.append(" select a.job_id jobId,b.cv_name cvName,a.name jobName,d.cv_name jobContacter,a.ar_dt arDt,e.account owner,b1.name custName,a.creator,b.mobile,b.tel phone,b.email  from (select distinct a1.creator,a5.ar_dt,a0.cv_id,a0.job_id,a2.name,a2.hr_contacter,a2.ar_contacter,a2.cust_id from contract_job_cv_recom a0,contract_job_cv_offer a1,contract_job a2,account_pn a3,account_ar a5 where a3.nbr=a5.pn_nbr and a3.offer_id=a1.id and a0.id=a1.recom_id and a0.job_id=a2.id) a ");
		sql.append(" left join  cv b on a.cv_id=b.id ");
		//customer_contacter
		sql.append(" left join  (select t2.id,t1.name,t1.full_name from company t1,customer t2 where t1.id=t2.company_id ) b1 on a.cust_id=b1.id ");
		sql.append(" left join  (select a0.id,a0.cv_name,a1.role,a1.cust_id from cv a0,customer_contacter a1 where a0.id=a1.cv_id ) d on a.cust_id=d.cust_id and d.role='").append(ConstantDic.CUSTOMER_CONTACTER_TYPE_JOB_CONTACTER).append("' ");
		sql.append(" left join (select  t1.job_id,min(t2.account) account from   contract_job_search_project t1,search_project t2 where t1.project_id=t2.id group by t1.job_id ) e on a.job_id=e.job_id ");
		//sql.append(" left join (select cv_id,group_concat((nbr) separator ',') as mobile from cv_contact_detail where contact_type='").append(ConstantDic.ContactType_MOBILE).append("' group by cv_id) f on a.cv_id=f.cv_id ");
		//sql.append(" left join (select cv_id,group_concat((concat(if(nbr_ext1 is null,'','('),ifnull(nbr_ext1,''),if(nbr_ext1 is null,'',')'),ifnull(nbr,''),if(nbr_ext2 is null,'','-'),ifnull(nbr_ext2,''))) separator ',') as phone from cv_contact_detail where contact_type='").append(ConstantDic.ContactType_TEL).append("' group by cv_id) h on a.cv_id=h.cv_id ");
		//sql.append(" left join (select cv_id,group_concat((nbr) separator ',') as email from cv_contact_detail where contact_type='").append(ConstantDic.ContactType_EMAIL).append("' group by cv_id) i on a.cv_id=i.cv_id ");
		//sql.append(" left join (select cv_id,group_concat((tag) separator ',') as tag from cv_tag group by cv_id) j on a.cv_id=j.cv_id ");
		sql.append(" ) comcv ");
		List values = new ArrayList();
		
		sql.append(" where comcv.jobId=? ");
		values.add(query.getJobId());
		
		//return super.getJdbcDao().createPage(query, values, mapper, pageMaxSize, pageNumber)
		return super.getJdbcDao().createPage(sql.toString(),values.toArray(), new MapObjectRowMapper(), query.getPageSize(), query.getPageNumber());
	}
	
	public Page findPage(AccountArQuery query) {
        //XsqlBuilder syntax,please see http://code.google.com/p/rapid-xsqlbuilder
        // [column]为字符串拼接, {column}为使用占位符. [column]为使用字符串拼接,如username='[username]',偷懒时可以使用字符串拼接 
        // [column] 为PageRequest的属性
        
		
		    Class[] os={AccountArQuery.class};
    		Class property= org.springframework.beans.BeanUtils.findPropertyType(query.getField_type(), os);
        if (property!=null){
        	 if ("java.lang.String".equals(property.getName())){
	    			
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(),query.getQuery());
	    			
	    		}
	    		else if ("java.lang.Integer".equals(property.getName())){
	    			
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseInteger(query.getQuery(), 0));
	    			
	    		}
	    		else if ("java.lang.Double".equals(property.getName())){
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseDouble(query.getQuery(), 0));
	    		}
	    		else if ("java.util.Date".equals(property.getName())){
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), DateHelper.toDate(query.getQuery(), DateHelper.DEFAULT_DATE_FORMAT));
	    		}
        }	 
        //生成sql2的原因是为了不喜欢使用xsqlbuilder的同学，请修改生成器模板，删除本段的生成
        StringBuilder sql2 = new StringBuilder("select t from AccountAr t where 1=1 ");
          if(isNotEmpty(query.getId())) {
            sql2.append(" and  t.id = :id ");
        }
        if(isNotEmpty(query.getPnNbr())) {
            //sql2.append(" and  t.pnNbr like :pnNbr ");
            //query.setPnNbr("%"+query.getPnNbr()+"%"); 
        	sql2.append(" and  t.pnNbr = :pnNbr ");
            //query.setPnNbr(query.getPnNbr());

        }
          if(isNotEmpty(query.getAmount())) {
            sql2.append(" and  t.amount = :amount ");
        }
        if(isNotEmpty(query.getArDtBegin())) {
            sql2.append(" and  t.arDt >= :arDtBegin ");
        }
        if(isNotEmpty(query.getArDtEnd())) {
            sql2.append(" and  t.arDt <= :arDtEnd ");
        }
        if(isNotEmpty(query.getConfirmDtBegin())) {
            sql2.append(" and  t.confirmDt >= :confirmDtBegin ");
        }
        if(isNotEmpty(query.getConfirmDtEnd())) {
            sql2.append(" and  t.confirmDt <= :confirmDtEnd ");
        }
        if(isNotEmpty(query.getConfirmor())) {
            sql2.append(" and  t.confirmor like :confirmor ");
            query.setConfirmor("%"+query.getConfirmor()+"%"); 

        }
        if(isNotEmpty(query.getCurrency())) {
            sql2.append(" and  t.currency like :currency ");
            query.setCurrency("%"+query.getCurrency()+"%"); 

        }
          if(isNotEmpty(query.getArAmount())) {
            sql2.append(" and  t.arAmount = :arAmount ");
        }
          if(isNotEmpty(query.getStatus())) {
            sql2.append(" and  t.status = :status ");
        }
        if(isNotEmpty(query.getComment())) {
            sql2.append(" and  t.comment like :comment ");
            query.setComment("%"+query.getComment()+"%"); 

        }
        if(isNotEmpty(query.getCreator())) {
            sql2.append(" and  t.creator like :creator ");
            query.setCreator("%"+query.getCreator()+"%"); 

        }
        if(isNotEmpty(query.getCreateDtBegin())) {
            sql2.append(" and  t.createDt >= :createDtBegin ");
        }
        if(isNotEmpty(query.getCreateDtEnd())) {
            sql2.append(" and  t.createDt <= :createDtEnd ");
        }
        if(isNotEmpty(query.getModifier())) {
            sql2.append(" and  t.modifier like :modifier ");
            query.setModifier("%"+query.getModifier()+"%"); 

        }
        if(isNotEmpty(query.getModifiedDtBegin())) {
            sql2.append(" and  t.modifiedDt >= :modifiedDtBegin ");
        }
        if(isNotEmpty(query.getModifiedDtEnd())) {
            sql2.append(" and  t.modifiedDt <= :modifiedDtEnd ");
        }
        if(isNotEmpty(query.getSortColumns())) {
            sql2.append(" order by :sortColumns ");
        }	
        
		return pageQuery(sql2.toString(),query);
	}
	

}
